<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0">左边</el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录表单 -->
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
          <h1>Hello</h1>
          <h2>欢迎来到Daniel后台管理系统</h2>
          <el-form-item prop="userName">
            <el-input :prefix-icon="User" type="text" placeholder="请输入账号" v-model="loginForm.userName"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password"  placeholder="请输入密码"
              v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-button class="login_btn" type="primary" @click="signIn">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang='ts'>
// 引入Element Plus 图标
import { User, Lock } from '@element-plus/icons-vue';
import { ref, reactive } from 'vue';
import useUserStore from '@/store/modules/user';
import { useRouter,useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getTimeDesc } from '@/utils/time';

const $router = useRouter();
const $route = useRoute();
const loginFormRef = ref()
const userStore = useUserStore();

const loginForm = reactive({
  userName: '',
  password: ''
});

function checkPwd(rule: any, value: any, callback: any) {
  // console.log('checkPwd', rule, value, callback);

  if (value === '') {
    callback(new Error('请输入密码'));
  } else if (value.length < 6 || value.length > 20) {
    callback(new Error('密码长度在 6 到 20 个字符之间'));
  } else {
    callback();
  }
}

const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符之间', trigger: 'change' }
  ],
  password: [
    { trigger: 'change', validator: checkPwd }
  ]
})



// 登录
async function signIn() {

  try {
    // 校验表单
    await loginFormRef.value.validate((valid: any) => {
      if (!valid) {
        throw new Error('表单验证失败，请检查输入');
      }
    })

    await userStore.login(loginForm);

    ElNotification({
      title: '登录成功',
      message: '欢迎回来,' + getTimeDesc(),
      type: 'success',
      duration: 1000
    });

    // let redirect = $router.currentRoute.value.query.redirect as string;
    let redirect = $route.query.redirect as string;
    $router.push({ path: redirect || '/' });

  } catch (error) {
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      // message: (error as Error).message || '登录失败，请重试',
      message: JSON.stringify(error) || '登录失败，请重试',
      duration: 1000
    })
  }

  // 清空登录表单
  // loginForm.username = '';
  // loginForm.password = '';
}

</script>
<style scoped lang="scss">
//  @use '@/styles/index.scss' as base; // 引入单独的scss文件

.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  color: white;

  .login_form {
    position: relative;
    width: 60%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;
    // border: 1px solid rgba(255, 255, 255, 0.5);
  }

  h1 {
    font-size: 40px;
  }

  h2 {
    font-size: 20px;
    margin: 20px 0;
  }

  .login_btn {
    width: 100%;
    font-size: 15px;
  }
}
</style>